﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using TSqlServer;
public partial class log_LogInfo : System.Web.UI.Page
{
    public string a;
    protected void Page_Load(object sender, EventArgs e)
    {
        a = FreeTextBox1.ClientID + ".InnerHTML";
        FreeTextBox1.ImageGalleryPath =Server.UrlEncode( "images\\" + Session["userid"].ToString());
        if (!IsPostBack)
        {
            BindControl();
        }
        if (Request["operation"] != null)
        {
            switch (Request["operation"].ToString())
            {
                case "save":
                    Save(Request["id"].ToString());
                    break;
                case "edit":
                    trtempsave.Visible = false;
                    break;
                case"checkchange":
                    CheckChange(Request["id"].ToString());
                    break;
                case "findF":
                    findF();
                    break;
            }
        }
        
    }
    public void CheckChange(string id)
    {
        TLogInfo oLogInfo = new TLogInfo(id);
        oLogInfo.Select();
        Response.Clear();
        if (oLogInfo.Title == Request["title"].ToString() && oLogInfo.Content == Request["content"].ToString())
            Response.Write("nochanged");
        else
            Response.Write("ischanged");
        Response.Flush();
        Response.End();
        
    }

    public void BindControl()
    {
        string sSql = "select * from t_log_type where inman='"+Session["userid"].ToString()+"' order by sortnum";
        TSqlDBReader myReader = new TSqlDBReader(sSql);
        ddltype.DataTextField = "name";
        ddltype.DataValueField = "id";
        ddltype.DataSource = myReader.FDataReader;
        ddltype.DataBind();
        //关闭
        myReader.Free();
        if(Request["id"]!=null)
            if (Request["id"].ToString() != "")
            {
                TLogInfo oLogInfo = new TLogInfo(Request["id"].ToString());
                oLogInfo.Select();
                txttitle.Text = oLogInfo.Title;
                hfid.Value = oLogInfo.Id;
                rblauthority.SelectedValue = oLogInfo.Authority.ToString();
                ddltype.SelectedValue = oLogInfo.TypeId;
                FreeTextBox1.Text = oLogInfo.Content;


                sSql = "select a.log_id,a.friend_id,b.username from t_log_relationship as a " +
                       " left join t_user1 as b on b.iid=a.friend_id" + 
                       " where a.log_id='"+ oLogInfo.Id +"'";
                myReader = new TSqlDBReader(sSql);
                SqlDataReader dr = myReader.FDataReader;
                while (dr.Read())
                {
                    txtlf.InnerHtml = "<span id='" + dr["friend_id"].ToString() + "' style='border-style:solid; border-color:red; border-width:1px;'>&nbsp;" + dr["username"].ToString() + "&nbsp;<a href='#' style='color:Red' onclick='javascript:delfriend($(this)," + dr["friend_id"].ToString() + ")' ><strong >×</strong></a></span>" + txtlf.InnerHtml;
                    hffriend.Value += "," + dr["friend_id"].ToString();
                }
                dr.Close();
                myReader.Free();
                
            }
    }
    public void Save(string id)
    {
        string logid=id;
        if (id == "")
        {
            TLogInfo oLogInfo = new TLogInfo();
            oLogInfo.Title = Request["title"].ToString();
            oLogInfo.TypeId = Request["type"].ToString();
            oLogInfo.Authority = int.Parse(Request["authority"].ToString());
            oLogInfo.Content = Server.UrlDecode ( Request["content"].ToString());
            oLogInfo.Status =int.Parse( Request["savetype"].ToString());
            oLogInfo.InTime = DateTime.Now;
            oLogInfo.Id = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString().PadLeft(3, '0');
            logid=oLogInfo.Id;
            oLogInfo.InMan = Session["userid"].ToString();
            oLogInfo.Insert();
            Response.Clear();
            Response.Write(oLogInfo.Id);
            Response.Flush();
            Response.Close();
        }
        else
        {
            TLogInfo oLogInfo =new TLogInfo(id);
            oLogInfo.Select();
            oLogInfo.Title = Request["title"].ToString();
            oLogInfo.TypeId = Request["type"].ToString();
            oLogInfo.Authority = int.Parse(Request["authority"].ToString());
            oLogInfo.Content =Server.UrlDecode( Request["content"].ToString());
            oLogInfo.Status = int.Parse(Request["savetype"].ToString());
            oLogInfo.InTime = DateTime.Now;
            oLogInfo.InMan = Session["userid"].ToString();
            oLogInfo.Update();
            Response.Clear();
            Response.Write(oLogInfo.Id);
            Response.Flush();
            Response.Close();

        }
        if (Request["hffriend"] == null)
            return;

        string[] friendlist = Request["hffriend"].ToString().Split(',');
        TLogRelationship.DeleteByLogId(logid);
        if (friendlist.Length >0)
        {
            foreach (string str in friendlist)
            {
                if (str.Trim() != "")
                {
                    TLogRelationship tlr = new TLogRelationship();
                    tlr.Friend_Id = str.Trim();
                    tlr.InMan = "";
                    tlr.InTime = DateTime.Now;
                    tlr.Log_Id = logid;
                    tlr.Insert();
                }
            }
        }

    }

    public void findF()
    {
        string temp = "";
        string ids = "";

        TSqlDB tdb = new TSqlDB();
        tdb.OpenRset("select iid as id,username as name from t_user1 where status='0'");
        DataTable dt = new DataTable();
        dt = tdb.DataSource.Table;
        tdb.Free();
      
        //dt.Columns.Add(new DataColumn("name"));
        //dt.Columns.Add(new DataColumn("id"));
        //DataRow dr = dt.NewRow();
        //dr["id"] = "1";
        //dr["name"] = "test1";
        //dt.Rows.Add(dr);
        //dr = dt.NewRow();
        //dr["id"] = "2";
        //dr["name"] = "test2";
        //dt.Rows.Add(dr);
        //dr = dt.NewRow();
        //dr["id"] = "3";
        //dr["name"] = "test3";
        //dt.Rows.Add(dr);
        //dr = dt.NewRow();
        //dr["id"] = "4";
        //dr["name"] = "test4";
        //dt.Rows.Add(dr);
        DataView dv=dt.DefaultView ;
        if (Request["content"] != null)
        {

            if (Request["ids"] != null && Request["ids"].ToString() != "")
            {
                ids = Request["ids"].ToString();
                if (ids.Trim().StartsWith(","))
                {
                    ids = Request["ids"].ToString().Substring(ids.IndexOf(",") + 1);
                    ids = ids.Replace(",","','");
                }

                dv.RowFilter =  " id not in ('" + ids + "')";
            }

            
            for (int i = 0; i < dv.Count  ; i++)
            {
                if (Request["content"].ToString().IndexOf(dv[i]["name"].ToString()) != -1)
                {
                    temp +=dv[i]["id"].ToString() + "," + dv[i]["name"].ToString() + ";";
                }
                
            }
        }
        Response.Clear();
        Response.Write(temp);
        Response.Flush();
        Response.Close();
        
    }

}
